<?php
class ServiceMediator{
	
	private $searchArray=array();
	private $db;
	
	// Attribut für den in der Bootstrap.ini definiterten Datenbankadapter
			
	public function ServiceMediator($searchArray){
		
		$this->searchArray=$searchArray;
		
		// Hole den standardmäßigen DB Adapter (definiert in der Boootstrap.ini)
		$this->db = Zend_Db_Table::getDefaultAdapter();
		
		
			
	}
	
	private function getProviderTemplate() {
		
		//Erstellung des Provider Templates
		
		$providerTemplate = array();
		$providerTemplate['ServiceProvider_ID']='';
		$providerTemplate['ServiceProvider_name']='';
		$providerTemplate['ServiceProvider_info']='';
		$providerTemplate['profiles']=array();
		
		
		
		return $providerTemplate;
		
	}
	
	private function giveProfilesTemplate(){
		$profiles = array();
		$profiles['ServiceProfile_ID']='';
		$profiles['ServiceProfile_name']='';
		$profiles['ServiceProfile_description']='';
		$profiles['fk_Offers']='';
		$profiles['offer']=array();
		
		return $profiles;
		
	}
	
	public function giveMatch(){
		
		
		$resultArray = array();
									
		// Generiere Array, das alle ServiceProvider mit passendem DIENSTangebot enthält.
		
		/*
		 	SELECT *
			FROM ServiceProvider AS sp 
				JOIN ServiceProvider_has_ServiceProfile AS shp ON sp.ServiceProvider_ID = shp.ServiceProvider_ID 
				JOIN ServiceProfile AS p ON shp.ServiceProfile_ID = p.ServiceProfile_ID 
				JOIN ServiceType AS st ON p.fk_ServiceType = st.ServiceTypes_ID	
			WHERE serviceBeschreibung = 'Ausgewählter Dienst(!)';
		 */
		
		
		$select = new Zend_Db_Select($this->db);
		$select->from(array('sp'=>'ServiceProvider'))
			->join(array('shp'=>'ServiceProvider_has_ServiceProfile'), 'sp.ServiceProvider_ID = shp.ServiceProvider_ID')
			->join(array('p'=>'ServiceProfile'), 'shp.ServiceProfile_ID = p.ServiceProfile_ID' )
			->join(array('st'=>'ServiceType'), 'p.fk_ServiceType = st.ServiceTypes_ID')
			->where('serviceBeschreibung =' .'"' . $this->searchArray['serviceType']. '"' )
			->order('sp.ServiceProvider_ID');
			
	
		
		
		
			
	
		$providerSet = $this->db->fetchAll($select);
		
		
						
		
		
		// Hole das Sicherheitsangebot zu den jeweiligen Providern
		
		foreach ($providerSet as $key => $value) {
			
			$select = new Zend_Db_Select($this->db);
			$select -> from('Offers')
					-> where('Offers_ID =' .'"' . $providerSet[$key]['fk_Offers'] . '"');
					
			$providerOffer = $this->db->fetchAll($select);
			
			$providerSet[$key]['offer']= $providerOffer[0]; 
			
			// Lösche nicht benötigte Daten aus dem Array
			unset($providerSet[$key]['offer']['Offers_ID']);
			
		}
		
		
		
		
		$provider = $this->getProviderArray($providerSet);
		
		
		
		// Modifiziere das Array $providerSet['offer'] so, dass nur relevante Daten darinstehen
		
		foreach ($provider as $pkey => $p) {
			foreach ($p['profiles'] as $key => $profile) {
				$profile['offer']=$this->modifieOfferArray($profile['offer']);
				$p['profiles'][$key]=$profile;
			}
				$provider[$pkey] = $p;	
		}
		
		
		//Zend_Debug::dump($provider);
					
		// Vergleiche das Angebot aus $providerSet['offer'] mit den Such-Vorgaben aus $searchArray
		
		foreach ($provider as $pkey => $p) {
		
			$modifiedArray = $this->modifieSearchArray(); // Enthält jetzt nur die relevanten Werte
			// Vergleicht die beiden Arrays
			
			foreach ($p['profiles'] as $key => $profile) {
			
				$mismatch = array_diff_assoc($providerSet[$key]['offer'], $modifiedArray);
				
				$misMatchCount = count($mismatch);
				$matchProzent = round(100- (($misMatchCount/count($modifiedArray))*100));
			
				$resultArray[$pkey]['ServiceProvider_ID'] = $p['ServiceProvider_ID'];
				$resultArray[$pkey]['ServiceProvider_name'] = $p['ServiceProvider_name'];
				$resultArray[$pkey]['profiles'][$key] = $profile;
				
				$resultArray[$pkey]['profiles'][$key]['matchProzent']=$matchProzent;
				$resultArray[$pkey]['profiles'][$key]['mismatchArray']=$mismatch;
				
				unset($resultArray[$pkey]['profiles'][$key]['offer']);	
			}
				
				
		}
		
		
		return $resultArray;
		
			
		
	}

	/**
	 * 
	 * Diese Funktion filtert aus dem searchArray die für die Suchanfrage relevanten Inhalte
	 */
	private function modifieSearchArray() {
		
		
		if ($this->searchArray['serviceType']== "Infrastructure as a Service Dienste") {
			
			return array_slice($this->searchArray['constraints'], 0, 25);
		}
		elseif ($this->searchArray['serviceType']== 'Platform as a Service Dienste'){
			return array_slice($this->searchArray['constraints'], 0, 26);
		}
		else {
			return array_slice($this->searchArray['constraints'], 0, 29);
		}
	}
	
	/**
	 * 
	 * Diese Funktion filtert aus dem offerArray des ServiceProviders die relevanten Suchfelder heraus.
	 * @param array $offerArray
	 */
	private function modifieOfferArray($offerArray){
		
	if ($this->searchArray['serviceType']== "Infrastructure as a Service Dienste") {
			
			$arr1 = array_slice($offerArray, 11, 18); // Dienst-Übergreifende Vorgaben und Compliance
			$arr2 = array_slice($offerArray, 4, 7); // IAAS Vorgaben
			return array_merge($arr1, $arr2);
		}
		elseif ($this->searchArray['serviceType']== 'Platform as a Service Dienste'){
			$arr1 = array_slice($offerArray, 11, 18); // Dienst-Übergreifende Vorgaben und Compliance
			$arr2 = array_slice($offerArray, 3, 8); // IAAS, PAAS 
			
			
			return array_merge($arr1, $arr2);
			
		}
		else {
			$arr1 = array_slice($offerArray, 11, 18); // Dienst-Übergreifende Vorgaben und Compliance
			$arr2 = array_slice($offerArray, 0, 11); // IAAS, PAAS und SAAS
			
			return array_merge($arr1, $arr2);
		}
		
	}
	
	/**
	 * 
	 * Formt das providerSet Array so um, dass jedes Profil dem zugehörigen Provider zugeordnet wird.
	 * @param array $providerSet
	 */
	private function getProviderArray($providerSet) {
		
		$result = array();
		
		
		$aktID = 0;
		
		foreach ($providerSet as $key => $value) {
					
			if($aktID != $value['ServiceProvider_ID']){
			
				if(isset($neuArray)) $result[] = $neuArray;
				
			$aktID = $value['ServiceProvider_ID'];
			$neuArray = $this->getProviderTemplate();
			
				foreach ($neuArray as $providerKey => $providerValue) {
					$neuArray[$providerKey]=$value[$providerKey];
				}
			
			
			}
			
			$neuProfiles = $this->giveProfilesTemplate();
			
			foreach ($neuProfiles as $profilesKey => $profilesValue) {
				$neuProfiles[$profilesKey] = $value[$profilesKey];
			}
			
			$neuArray['profiles'][] = $neuProfiles;
			
			
			
			
		}
		
		if(isset($neuArray)) $result[] = $neuArray;
			
	return $result;
	}
		
		
}